package com.keda;


import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import javax.naming.directory.SearchResult;
import java.io.IOException;

public class test_tiaojian_Query {
    public static void main(String[] args) throws IOException {

        //创建es 客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );

        // 条件查询数据
        SearchRequest request = new SearchRequest();

        //index的复数是indices或indexes。
        request.indices("user");


        //匹配user  下的任何条件


   //     request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));

     //    request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",20)));


       //分页查询
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //(当前页-1)*每页显示条数
        builder.from(0);
        builder.size(2);
        request.source(builder);


        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);


        //获取  所有的数据
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }


        //关闭es客户端
        try {
            esClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
